package com.zhang.learn.service.strategy.kpi;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zxl
 * @Date: 2024/12/6 17:41
 */
public class KpiCalculatorFactory {
    /**
     * 缓存计算KPI的策略
     */
    private static final Map<Integer, KpiCalculatorStrategy> kpiCalculatorStrategyMap = new HashMap<>();

    /**
     * KPI 策略的注册
     * @param type 计算kpi的类型
     * @param kpiCalculatorStrategy 具体的策略
     */
    public static void register(Integer type, KpiCalculatorStrategy kpiCalculatorStrategy) {
        kpiCalculatorStrategyMap.put(type, kpiCalculatorStrategy);
    }

    /**
     * 根据类型获取kpi计算的策略
     * @param type 类型
     * @return kpi计算的策略
     */
    public static KpiCalculatorStrategy getKpiCalculatorStrategy(Integer type) {
        KpiCalculatorStrategy kpiCalculatorStrategy = kpiCalculatorStrategyMap.get(type);
        if (kpiCalculatorStrategy == null) {
            return kpiCalculatorStrategyMap.get(ComputedType.MONTH.getValue());
        }
        return kpiCalculatorStrategy;
    }
}
